package com.googlecode.openauth.web.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;

import com.googlecode.openauth.web.config.CommonProperty;
import com.googlecode.openauth.web.dao.IActionDao;
import com.googlecode.openauth.web.dao.IRoleActionDao;
import com.googlecode.openauth.web.dao.IRoleDao;
import com.googlecode.openauth.web.entity.ActionInfo;
import com.googlecode.openauth.web.entity.ActionPageBean;
import com.googlecode.openauth.web.entity.RoleInfo;
import com.googlecode.openauth.web.entity.UserInfo;
import com.googlecode.openauth.web.util.Util;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UserActionAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 9115492660719307189L;

	private IActionDao actionDao;
	private IRoleActionDao roleActionDao;
	private int actionIds[];
	private List<ActionInfo> actlist;
	private int actionId;
	private String uuid;
	private String actionName;
	private String actionDesc;
	private String oper = "list";
	private IRoleDao roleDao;

	public IRoleDao getRoleDao() {
		return roleDao;
	}

	public UserActionAction() {
		System.out.println("hello");
	}

	public void setRoleDao(IRoleDao roleDao) {
		this.roleDao = roleDao;
	}

	public IRoleActionDao getRoleActionDao() {
		return roleActionDao;
	}

	public void setRoleActionDao(IRoleActionDao roleActionDao) {
		this.roleActionDao = roleActionDao;
	}

	@Override
	public String execute() throws Exception {
		ActionInfo action = new ActionInfo();
		action.setRoleId(this.getCurRoleId());
		actlist = actionDao.getActions(action);
		System.out.println("hello");
		actionPageBean = new ActionPageBean<ActionInfo>();
		actionPageBean.setData(actlist);
		return SUCCESS;
	}

	private UserInfo getCurUser(){
		ActionContext context = ActionContext.getContext();
		Map<String, Object> session = context.getSession();
		Object obj = session.get("user");
		UserInfo result = new UserInfo();
		result.setUserId(-1);
		if(obj == null){
			return result;
		}
		
		if(!(obj instanceof UserInfo)){
			return result;
		}
		
		result = (UserInfo) obj;
		return result;
	}
	
	private RoleInfo getCurRole(){
		RoleInfo result = new RoleInfo();
		result.setRoleId(-1);
		
		UserInfo userInfo = getCurUser();
		List<RoleInfo> roles = userInfo.getRoleInfos();
		
		if(roles == null || roles.isEmpty()){
			return result;
		}
		
		result = roles.get(0);
		return result;
	}
	
	private int getCurRoleId() {
		RoleInfo role = getCurRole();
		int curRoleId = role.getRoleId();
		return curRoleId;
	}

	private ActionPageBean<ActionInfo> actionPageBean;
	private int currentPageNo = 1;
	private int prevPageNo = 1;
	private int nextPageNo = 1;
	private int totalCount = 0;
	private int pageNum = CommonProperty.PAGENUM;

	public String save() throws Exception {
		ActionInfo form = this.getForm();
		if (oper.equals("save")) {
			actionDao.insertAction(form);
			int action = form.getActionId();
			List<RoleInfo> rlist = new ArrayList<RoleInfo>();
			int curRoleId = this.getCurRoleId();
			rlist = roleDao.getChildRolesLeaf2Root(curRoleId);
			RoleInfo r = new RoleInfo();
			r.setRoleId(curRoleId);
			List<com.googlecode.openauth.web.entity.RoleAction> alist = new ArrayList<com.googlecode.openauth.web.entity.RoleAction>();
			for (RoleInfo role : rlist) {
				com.googlecode.openauth.web.entity.RoleAction ai = new com.googlecode.openauth.web.entity.RoleAction();
				ai.setRoleId(role.getRoleId());
				ai.setActionId(action);
				alist.add(ai);
			}
			roleActionDao.insertBatch(alist);
			resetForm();
			return SUCCESS;
		} else {
			actionDao.updateAction(form);
			resetForm();
			this.actionId = 0;
			return SUCCESS;
		}
	}

	public String init() throws Exception {

		if (oper.equals("save")) {
			return "new";
		} else {
			ActionInfo act = actionDao.getActionById(actionId);
			setForm(act);
			return "update";
		}
	}

	public String delete() throws Exception {
		this.roleActionDao.delRoleActionByActionId(actionIds[0]);
		actionDao.delActionById(actionIds[0]);
		resetForm();
		return SUCCESS;
	}

	public void setForm(ActionInfo act) {
		actionId = act.getActionId();
		uuid = act.getUuid();
		actionName = act.getActionName();
		actionDesc = act.getActionDesc();
	}

	public ActionInfo getForm() {
		ActionInfo act = new ActionInfo();
		act.setActionDesc(actionDesc);
		act.setActionId(actionId);
		act.setActionName(actionName);
		return act;
	}

	public String getFirstPage() throws Exception {
		
		RoleInfo curRole = getCurRole();
		
		int totalCount = actionDao.getAllActionsCount(curRole);
		actionPageBean = new ActionPageBean<ActionInfo>();
		actionPageBean.setTotalCount(totalCount);

		ActionPageBean<ActionInfo> pageBean = getPageBean();
		pageBean.setTotalCount(totalCount);
		pageBean.setPrevPageNo(1);
		pageBean.setCurrentPageNo(1);
		Util util = new Util();
		int maxPageNo = util.getMaxPageNo(pageBean);
		if (maxPageNo <= 1) {
			pageBean.setNextPageNo(1);
		} else {
			pageBean.setNextPageNo(2);
		}

		int offset = 0;
		int limit = pageBean.getPageNum();
		RowBounds rowBounds = new RowBounds(offset, limit);
		setPageBean(pageBean);
		List<ActionInfo> data = actionDao.getAllActions(curRole, rowBounds);
		actionPageBean.setData(data);
		return SUCCESS;
	}

	public String getPrevPage() throws Exception {
		
		RoleInfo curRole = getCurRole();
		
		int totalCount = actionDao.getAllActionsCount(curRole);
		actionPageBean = new ActionPageBean<ActionInfo>();
		actionPageBean.setTotalCount(totalCount);

		ActionPageBean<ActionInfo> pageBean = getPageBean();
		pageBean.setTotalCount(totalCount);
		Util util = new Util();
		RowBounds rowBounds = util.calcPrevRowBounds(pageBean);
		setPageBean(pageBean);
		List<ActionInfo> data = actionDao.getAllActions(curRole, rowBounds);
		actionPageBean.setData(data);
		return SUCCESS;
	}

	public String getNextPage() throws Exception {
		
		RoleInfo curRole = getCurRole();
		
		int totalCount = actionDao.getAllActionsCount(curRole);
		actionPageBean = new ActionPageBean<ActionInfo>();
		actionPageBean.setTotalCount(totalCount);

		ActionPageBean<ActionInfo> pageBean = getPageBean();
		pageBean.setTotalCount(totalCount);
		Util util = new Util();
		RowBounds rowBounds = util.calcNextRowBounds(pageBean);
		setPageBean(pageBean);
		List<ActionInfo> data = actionDao.getAllActions(curRole, rowBounds);
		actionPageBean.setData(data);
		return SUCCESS;
	}

	public String getLastPage() throws Exception {
		
		RoleInfo curRole = getCurRole();
		
		int totalCount = actionDao.getAllActionsCount(curRole);
		actionPageBean = new ActionPageBean<ActionInfo>();
		actionPageBean.setTotalCount(totalCount);

		ActionPageBean<ActionInfo> pageBean = getPageBean();
		pageBean.setTotalCount(totalCount);
		Util util = new Util();
		int maxPageNo = util.getMaxPageNo(pageBean);
		pageBean.setCurrentPageNo(maxPageNo);
		pageBean.setNextPageNo(maxPageNo);

		if (maxPageNo <= 1) {
			pageBean.setPrevPageNo(1);
		} else {
			pageBean.setPrevPageNo(maxPageNo - 1);
		}
		int limit = pageBean.getPageNum();
		int offset = (maxPageNo - 1) * limit;
		RowBounds rowBounds = new RowBounds(offset, limit);
		setPageBean(pageBean);
		List<ActionInfo> data = actionDao.getAllActions(curRole, rowBounds);
		actionPageBean.setData(data);
		return SUCCESS;
	}

	public void resetForm() {
		actionId = 0;
		uuid = "";
		actionName = "";
		actionDesc = "";
	}

	// ///////////////////////////////////////////////////

	public List<ActionInfo> getActlist() {
		return actlist;
	}

	public void setActlist(List<ActionInfo> actlist) {
		this.actlist = actlist;
	}

	public IActionDao getActionDao() {
		return actionDao;
	}

	public void setActionDao(IActionDao actionDao) {
		this.actionDao = actionDao;
	}

	public int[] getActionIds() {
		return actionIds;
	}

	public void setActionIds(int[] actionIds) {
		this.actionIds = actionIds;
	}

	public int getActionId() {
		return actionId;
	}

	public void setActionId(int actionId) {
		this.actionId = actionId;
	}

	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public String getActionName() {
		return actionName;
	}

	public void setActionName(String actionName) {
		this.actionName = actionName;
	}

	public String getActionDesc() {
		return actionDesc;
	}

	public void setActionDesc(String actionDesc) {
		this.actionDesc = actionDesc;
	}

	public String getOper() {
		return oper;
	}

	public void setOper(String oper) {
		this.oper = oper;
	}

	public ActionPageBean<ActionInfo> getActionPageBean() {
		return actionPageBean;
	}

	public void setActionPageBean(ActionPageBean<ActionInfo> actionPageBean) {
		this.actionPageBean = actionPageBean;
	}

	public ActionPageBean<ActionInfo> getPageBean() {
		ActionPageBean<ActionInfo> bean = new ActionPageBean<ActionInfo>();
		bean.setCurrentPageNo(currentPageNo);
		bean.setPrevPageNo(prevPageNo);
		bean.setNextPageNo(nextPageNo);
		bean.setTotalCount(totalCount);
		bean.setPageNum(pageNum);
		return bean;
	}

	public void setPageBean(ActionPageBean<ActionInfo> bean) {
		currentPageNo = bean.getCurrentPageNo();
		prevPageNo = bean.getPrevPageNo();
		nextPageNo = bean.getNextPageNo();
		totalCount = bean.getTotalCount();
	}

	public int getCurrentPageNo() {
		return currentPageNo;
	}

	public void setCurrentPageNo(int currentPageNo) {
		this.currentPageNo = currentPageNo;
	}

	public int getPrevPageNo() {
		return prevPageNo;
	}

	public void setPrevPageNo(int prevPageNo) {
		this.prevPageNo = prevPageNo;
	}

	public int getNextPageNo() {
		return nextPageNo;
	}

	public void setNextPageNo(int nextPageNo) {
		this.nextPageNo = nextPageNo;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

}
